<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/7/22 0022
 * Time: 10:44
 */

namespace framework\library;

use Loader;

class App extends Container
{
    // 根目录
    protected $rootPath;

    // 框架目录
    protected $fwPath;

    // 应用目录
    protected $appPath;

    // 路由目录
    protected $routePath;

    // 配置目录
    protected $configPath;

    public function __construct($appPath = "")
    {
        $this->path($appPath);
    }

    public function path($appPath = "")
    {
        $appPath || $appPath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . "app" . DIRECTORY_SEPARATOR;

        $this->appPath = $appPath;
        return $this;
    }

    public function test()
    {
        $arr = ["name" => "bqs", "age" => 55, "is_login" => 1];
        $preFileName = date('Ymd', time());
        $path = "logs/temp/" . date('Ym', time()) . '/';
        if (!is_dir($path)) {
            mkdir($path, 0755, true);
        }
        $ext = '.log';
        $filePath = $path . $preFileName . $ext;
        $content = var_export($arr, true);
        $tmp = "\n\n================= " . date('Y-m-d H:i:s') . " ===================\n\n";
        $content = $tmp . $content;
        //echo $filePath . "\r\n";

        file_put_contents($filePath, $content);
    }

    // 运行
    public function run()
    {
        $initialized = false;
        if ($initialized) {
            return;
        }

        $this->initialize();

        // 路由初始化
        $this->routeInit();

        // 请求分发
        //$response = $this->dispatch->run($this, $this->request, $this->route);

        // 中间件处理
        $request = $this->request;
        $route = $this->route;
        $app = $this;
        $dispatch = $this->dispatch;
        $this->middleware->import(function ($req, $next) use ($app, $request, $route, $dispatch) {
            return $dispatch->run($app, $request, $route);
        });

        $response = $this->middleware->dispatch($request);
        $initialized = true;

        return $response;
    }

    // 系统初始化
    public function initialize()
    {
        $this->rootPath = Loader::getRootPath();
        $this->fwPath = $this->rootPath . "framework" . DIRECTORY_SEPARATOR;
        $this->routePath = $this->rootPath . "route" . DIRECTORY_SEPARATOR;
        $this->configPath = $this->rootPath . "config" . DIRECTORY_SEPARATOR;

        // 加载config文件
        if (is_dir($this->configPath)) {
            $fileList = glob("$this->configPath/*.php");
            foreach ($fileList as $file) {
                $this->config->load($file);
            }
        }

        // 加载共用自定义函数
        if (is_file($this->appPath . "common.php")) {
            include $this->appPath . "common.php";
        }

        // 加载中间件(无效)
//        if (is_file($this->appPath . "middleware.php")) {
//            $middleware = include $this->appPath . "middleware.php";
//            $this->middleware->add($middleware);
//        }

        // 加载配置项中间件
        $middlewareConfig = $this->config->get('middleware');
        if ($middlewareConfig) {
            //$this->middleware->merge($middlewareConfig);
            $this->middleware->add($middlewareConfig);
        }

    }

    // 应用初始化
//    public function init($module = "")
//    {
//        // 加载app的配置项
//        $path = $this->appPath . $module;
//    }

    // 路由初始化
    public function routeInit()
    {
        $path = $this->routePath;
        if (is_dir($path)) {
            $files = glob($path . "*.php");
            foreach ($files as $key => $value) {
                include $value;
            }
        }
    }

    // 获取App路径
    public function getAppPath()
    {
        return $this->appPath . "app" . DIRECTORY_SEPARATOR;
    }

    // 获取根目录路径
    public function getRootPath()
    {
        return $this->rootPath;
    }

    // 获取路由目录
    public function getRoutePath()
    {
        return $this->routePath;
    }

    // 获取配置目录
    public function getConfigPath()
    {
        return $this->configPath;
    }


}